<template>
  <!-- 新增或修改岗位 -->
  <el-dialog :visible.sync="visible"
             v-if="visible"
             @close="dialogClose"
             :close-on-click-modal="false"
             :title="dialogStatus==='create'?'新增岗位':'修改岗位'"
             width="30%">
    <el-form :model="editJobForm"
             :rules="editJobFormRules"
             ref="editJobForm"
             label-width="90px">
      <el-form-item label="岗位名称"
                    prop="name">
        <el-select v-model="editJobForm.name"
                   v-if="editJobForm.dept_type=='2'||editJobForm.dept_type==2"
                   placeholder="请选择">
          <el-option v-for="item in projectJobArr"
                     :key="item.value"
                     :label="item.name"
                     :value="item.name">
          </el-option>
        </el-select>
        <el-input v-model="editJobForm.name"
                  v-else
                  autocomplete="off"
                  placeholder="请输入岗位名称"
                  style="width:100%!important"></el-input>
      </el-form-item>
      <el-form-item label="备注"
                    class="add_tips">
        <el-input v-model="editJobForm.remark"
                  autocomplete="off"
                  style="width:100%!important"></el-input>
      </el-form-item>
    </el-form>
    <div style="text-align:center;">
      <el-button @click="dialogClose"
                 id="back-style">返 回</el-button>
      <el-button @click="editJob"
                 type="primary"
                 id="save-style">保 存</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { backEvaluateApi } from '@/services/ProjectCenter'
import { organizationApi } from '@/services/CompanySetting'
import { standardCodeApi } from '@/services/StandardCode'
export default {
  data () {
    return {
      job_id: '',
      projectJobArr: [],//项目部岗位
      businessArea: [], //业务领域
      manageFunction: [], //管理职能
      visible: false,
      editJobForm: {
        name: '',
        remark: '',
        // business_field: '', //业务领域
        // manage_function: '', //管理职能
      },
      editJobFormRules: {
        name: [
          { required: true, message: '请输入岗位名称', trigger: 'blur' }
        ],
        // business_field: [
        //   { required: true, message: '请选择业务领域类别', trigger: ['blur', 'change'] }
        // ],
        // manage_function: [
        //   { required: true, message: '请选择管理职能类别', trigger: ['blur', 'change'] }
        // ]
      }
    }
  },
  props: {
    dialogVisible: Boolean,
    dialogForm: Object,
    dialogStatus: String,
    callback: Function
  },
  watch: {
    dialogVisible: {
      handler (newval, oldval) {
        this.visible = newval
      },
      deep: true
    },
    dialogForm: {
      async handler (newval, oldval) {
        let backToBackProjectJobArr = localStorage.getItem('backToBackProjectJobArr');
        if (backToBackProjectJobArr) {//存在
          this.projectJobArr = JSON.parse(backToBackProjectJobArr);//项目部岗位
        } else {
          this.getProJobData();//项目部下拉岗位
        }
        // this.getJobList();//岗位下拉
        this.editJobForm = Object.assign(this.editJobForm, JSON.parse(JSON.stringify(newval)))
        this.editJobForm.name = this.editJobForm.node_name;
        console.log('w=editJobForm=', this.editJobForm)
        this.editJobForm.node_type = '3'
      },
      deep: true
    },
    dialogStatus (newval, oldval) {
      if (newval === 'update') {
        this.editJobForm.name = this.editJobForm.name
        // this.detailjob()
      } else if (newval === 'create') {
        this.editJobForm.name = ''
        this.editJobForm.remark = ''
        // this.editJobForm.business_field = [] //业务领域
        // this.editJobForm.manage_function = [] //管理职能
      }
    }
  },
  methods: {
    getProJobData () {//项目部的岗位
      return standardCodeApi.getStandardCodeData({
        data: {
          code_type: 'projectJob'//项目部岗位
        }
      }).then((res) => {
        if (res.data) {
          let list = res.data.list;
          this.projectJobArr = list || [];
          console.log('项目部岗位res=', res)
          window.localStorage.setItem('backToBackProjectJobArr', JSON.stringify(this.projectJobArr));
        }
      }).catch((error) => {
        console.log(error)
      })
    },
    // getJobList () {//岗位下拉
    //   let compId = window.localStorage.comp_id || '';
    //   return backEvaluateApi.getJobList({
    //     params: {
    //       is_page: 0,
    //       company_id: compId,//单位主键值
    //       node_id: this.dialogForm.node_id
    //     }
    //   }).then(res => {
    //     if (res.data) {
    //       this.jobArr = res.data.list;
    //     }
    //   }).catch((error) => {
    //     console.log('error')
    //   })
    // },
    //管理职能选择框
    changManage (val) {
      this.editJobForm.manage_function = val
    },
    //业务领域选择框
    changBusiness (val) {
      this.editJobForm.business_field = val
    },
    dialogClose () {
      this.$refs.editJobForm.resetFields()
      this.$emit('update:dialogVisible', false)
      this.$emit('update:dialogStatus', '')
    },
    editJob () {
      this.$refs.editJobForm.validate((valid) => {
        if (valid) {
          if (this.dialogStatus === 'create') {
            this.createJob()
          } else if (this.dialogStatus === 'update') {
            this.updateJob()
          }
        }
      })
    },
    createJob () {
      this.$refs.editJobForm.validate((valid) => {
        if (valid) {
          // this.editJobForm.business_field = this.editJobForm.business_field.join(',')
          // this.editJobForm.manage_function = this.editJobForm.manage_function.join(',')
          // organizationApi.createOrg({
          backEvaluateApi.saveObs({
            data: this.editJobForm
          }).then(res => {
            if (res.code === 0) {
              this.$message.success('新增岗位成功')
              this.dialogClose()
              this.$emit('callback', 'success')
            }
          })
        }
      })
    },
    updateJob () {
      // organizationApi.updateJob({    
      backEvaluateApi.editObs({//修改
        data: this.editJobForm
      }).then(res => {
        if (res.code === 0) {
          this.$message.success('修改岗位成功')
          this.dialogClose()
          this.$emit('callback', 'success')
        }
      })
    },
    getBusinessField (arr) {//业务领域
      let newArr = [];
      if (arr.length == 1 && arr[0] == '') {
        newArr = []
      } else {
        newArr = arr
      };
      return newArr;
    },
    getManageFunc (arr) {//职能
      let newArr = [];
      if (arr.length == 1 && arr[0] == '') {
        newArr = []
      } else {
        newArr = arr
      };
      return newArr;
    },
    // //修改-岗位内容
    // detailjob () {
    //   organizationApi.detailJob({
    //     params: {
    //       job_id: this.editJobForm.id,
    //       // show_object: 0,
    //       // show_app: 0
    //     }
    //   }).then(res => {
    //     let businessField = res.data.business_field;//业务领域
    //     let manageFunc = res.data.manage_function;//职能
    //     this.editJobForm.business_field = this.getBusinessField(businessField);//业务范围
    //     this.editJobForm.manage_function = this.getManageFunc(manageFunc);//职能
    //   }, err => {
    //     console.log(err);
    //   })
    // },
  },
  created () {
    // this.getbusinessCode()
    // this.getmanageCode()
  },
  mounted () {
  }
}
</script>
<style scoped lang='scss'>
</style>
